package ca.etsmtl.logti.log619.lab05.filter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.internet.MimeMessage;

import ca.etsmtl.logti.log619.lab05.utilities.BayesFilterBuilder;
import ca.etsmtl.logti.log619.lab05.utilities.EmailSplicer;

public class SpammerFilter implements EmailFilter {

	public static String name = "SpammerFilter";
	private String commentString = "#";

	@Override
	public byte checkSpam(MimeMessage message) {

		File spammerlist;
		FileInputStream fis;
		InputStreamReader isr;
		BufferedReader br = null;
		ArrayList<String> spammers = new ArrayList<String>();

		int nbBlacklistedWordFound = 0;

		try {

			spammerlist = new File("data/filter/spammer.txt");
			fis = new FileInputStream(spammerlist);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);

			while (br.ready()) {

				String line = br.readLine();

				if (!line.substring(0, 1).equalsIgnoreCase(commentString)) {
					spammers.add(line.trim());
				}
			}
			boolean found = false;
			
			try
			{
				for (String spammer : spammers) {

						for(int i = 0; i < message.getFrom().length; i++)
						if (message.getFrom()[i] != null && message.getFrom()[i].toString().contains(spammer))
							found = true;
				
				}
			}catch(Exception e)
			{}
			if(found )
				return 100;
			else
				return CANT_SAY;

		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			
		} catch (Exception e) {
			e.printStackTrace();
		} 

		return CANT_SAY;
	}

	@Override
	public boolean enabled() {

		return true;
	}

	@Override
	public byte getConfidenceLevel() {
		return 100;
	}

	@Override
	public String getFilterName() {

		return name;
	}

	public void setCommentString(String commentString) {
		this.commentString = commentString;
	}

	public String getCommentString() {
		return commentString;
	}

}
